package com.aiyige.page.publish.courseschedule;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.page.publish.courseschedule.model.CourseScheduleModel;
import com.aiyige.richtexteditor.RichTextEditor;
import com.aiyige.richtexteditor.model.RTEModel;
import com.aiyige.utils.DialogUtil;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.widget.articleview.ArticleView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.CourseSchedulePage)
public class CourseSchedulePage extends AppCompatActivity {
    public static final String EXTRA_KEY_COURSE_SCHEDULE_DATA = "com.aiyige.extra.EXTRA_KEY_COURSE_SCHEDULE_DATA";
    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.titleActionBtn)
    Button titleActionBtn;
    @BindView(R.id.editor)
    RichTextEditor editor;
    @BindView(R.id.containerLayout)
    View containerLayout;
    @Autowired
    CourseScheduleModel courseScheduleModel = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_course_schedule);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        editor.onCreate(this);
        editor.setCallback(new RichTextEditor.Callback() {
            @Override
            public void notifyContentEmpty(boolean empty) {
                courseScheduleModel.getRteModel().setEmpty(empty);
            }

            @Override
            public void contentReceived(RTEModel model) {
                if (isFinishing()) return;
                courseScheduleModel.setRteModel(model);
                Intent data = new Intent();
                data.putExtra(EXTRA_KEY_COURSE_SCHEDULE_DATA, courseScheduleModel);
                setResult(RESULT_OK, data);
                finish();
            }
        });
        titleTv.setText(R.string.course_schedule);
        titleActionBtn.setText(R.string.finish);
        if (courseScheduleModel == null) {
            ToastX.show(R.string.unknown_error);
            finish();
            return;
        }
        editor.setData(courseScheduleModel.getRteModel());
        editor.loadEditor();
    }


    @Override
    protected void onResume() {
        editor.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        editor.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        editor.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        DialogUtil.makeConfirmExitDialog(this, R.string.confirm_exit_edit, R.string.confirm, R.string.cancel).show();
    }

    @OnClick({R.id.titleBackBtn, R.id.titleActionBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                DialogUtil.makeConfirmExitDialog(this, R.string.confirm_exit_edit, R.string.confirm, R.string.cancel).show();
                break;
            case R.id.titleActionBtn:
                editor.requestContent();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RichTextEditor.REQUEST_CODE_INSERT_IMAGE:
            case RichTextEditor.REQUEST_CODE_INSERT_VIDEO:
                editor.onActivityResult(requestCode, resultCode, data);
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }
}
